package com.jinzihan.web;

import com.jinzihan.entity.Classroom;
import com.jinzihan.entity.Order;
import com.jinzihan.entity.Page;
import com.jinzihan.entity.Student;
import com.jinzihan.service.ClassroomService;
import com.jinzihan.service.OrderService;
import com.jinzihan.service.StudentService;
import com.jinzihan.service.impl.ClassroomServiceImpl;
import com.jinzihan.service.impl.OrderServiceImpl;
import com.jinzihan.service.impl.StudentServiceImpl;
import com.jinzihan.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    ClassroomService classroomService = new ClassroomServiceImpl();
    StudentService studentService = new StudentServiceImpl();

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object student = req.getSession().getAttribute("student");
        Object manager = req.getSession().getAttribute("manager");
        if (student==null){
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return;
        }else if (manager!=null){
            return;
        }
        int classroomId = WebUtils.parseInt(req.getParameter("classroomId"), 0);
        int studentId = WebUtils.parseInt(req.getParameter("studentId"), 0);
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        Order order = new Order(null, studentId, 0, new Date(System.currentTimeMillis()), classroomId);
        orderService.createOrder(order);
        resp.sendRedirect(req.getContextPath() + "/client/clientClassroomServlet?action=page&pageNo=" + pageNo);
    }

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2 调用 ClassroomService.page(pageNo，pageSize)：Page 对象
        Page<Order> page = orderService.page(pageNo, pageSize);
        page.setUrl("orderServlet?action=page");
        //3 保存 Page 对象到 Request 域中
        req.setAttribute("page", page);
        //4 请求转发到 pages/manager/book_manager.jsp 页面
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }

    protected void myPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取请求的参数 pageNo 和 pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Student student = (Student) req.getSession().getAttribute("student");
        //2 调用 ClassroomService.page(pageNo，pageSize)：Page 对象
        Page<Order> page = orderService.myPage(student.getStudentId(), pageNo, pageSize);
        page.setUrl("orderServlet?action=myPage");
        //3 保存 Page 对象到 Request 域中
        req.setAttribute("page", page);
        //4 请求转发到 pages/manager/book_manager.jsp 页面
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req, resp);
    }

    protected void accept(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = WebUtils.parseInt(req.getParameter("orderId"), 0);
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        orderService.acceptOrder(orderId);
        req.getRequestDispatcher("/orderServlet?action=page&pageNo=" + pageNo).forward(req,resp);
    }

    protected void refuse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = WebUtils.parseInt(req.getParameter("orderId"), 0);
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        orderService.refuseOrder(orderId);
        req.getRequestDispatcher("/orderServlet?action=page&pageNo=" + pageNo).forward(req,resp);
    }

    protected void details(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int orderId = WebUtils.parseInt(req.getParameter("orderId"), 0);
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        req.setAttribute("pageNo",pageNo);
        Order order = orderService.queryOrderById(orderId);
        String username = studentService.queryUsernameById(order.getStudentId());
        Classroom classroom = classroomService.queryClassroomById(order.getClassroomId());
        req.setAttribute("studentId",order.getStudentId());
        req.setAttribute("studentName",username);
        req.setAttribute("classroomName",classroom.getName());
        Integer intMedium = classroom.getMedium();
        String medium;
        if (intMedium == 0){
            medium = "无";
        }else {
            medium = "有";
        }
        req.setAttribute("classroomMedium",medium);
        req.setAttribute("classroomCapacity",classroom.getCapacity());
        Integer intType = classroom.getType();
        String type;
        if (intType == 0){
            type = "普通教室";
        }else {
            type = "阶梯教室";
        }
        req.setAttribute("classroomType",type);
        req.getRequestDispatcher("/pages/manager/details.jsp").forward(req,resp);
    }
}